﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.ServiceModel;
using System.Web.UI.WebControls;
using AccountServiceReference;

public partial class UserControl_SignUp : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ltrCaptcha.Text = Utility.Generate.GenerateRandomCode(7);
        ddlSex.Items.Add(new ListItem("Male", "0"));
        ddlSex.Items.Add(new ListItem("FMale", "1"));
        }
        MultiView1.ActiveViewIndex = 0;
    }
    protected void lbtnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            var client = new AccountServiceClient();
            var result = client.SignUp(
                new Account()
                    {
                        Username = txtUsername.Text.Trim(),
                        Password = txtPassword.Text.Trim(),
                        Address = txtFname.Text.Trim(),
                        Birthday = Calendar1.SelectedDate,
                        Email = txtEmail.Text.Trim(),
                        Fname = txtFname.Text.Trim(),
                        Lname = txtLname.Text.Trim(),
                        Sex = ddlSex.SelectedValue == "0",
                        Phone = txtPhone.Text.Trim()
                    }
                );

            if (result && txtConfirmCaptcha.Text.Equals(ltrCaptcha.Text))
            {
                MultiView1.ActiveViewIndex = 1;
            }
            else if (!txtConfirmCaptcha.Text.Equals(ltrCaptcha.Text))
            {
                ltrmsg.Text = "Xac nhan captcha khong dung!";
            }
            else
            {
                ltrmsg.Text = "Tai khoan hoac email da ton tai";
            }
        }
        catch (FaultException<Account> faultException)
        {
            ltrmsg.Text = faultException.Message;
        }
        catch (Exception ex)
        {
            ltrmsg.Text = ex.Message;
        }

    }
}